package com.qf.shiro;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsAdminMapper;
import com.qf.mapper.DtsUserMapper;
import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsUser;
import com.qf.service.AdminService;
import com.qf.service.PermsService;
import com.qf.service.RoleService;
import com.qf.util.bcrypt.BCryptPasswordEncoder;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Wrapper;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

public class AdminAuthorizingRealm extends AuthorizingRealm {
    @Autowired
    private AdminService adminService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private PermsService permsService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        DtsAdmin dtsAdmin= (DtsAdmin) principalCollection.getPrimaryPrincipal();
        Set<String> rolesByRoleIds = roleService.findRolesByRoleIds(dtsAdmin.getRoleIds());
        Set<String> permsByRoleIds = permsService.findPermsByRoleIds(dtsAdmin.getRoleIds());

        SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
        info.setRoles(rolesByRoleIds);
        info.setStringPermissions(permsByRoleIds);

        return info;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        UsernamePasswordToken token=(UsernamePasswordToken)authenticationToken;
        String username = token.getUsername();
        String password=new String(token.getPassword());



        DtsAdmin dtsAdmin = adminService.findDtsAdminByUsername(username);
        System.out.println(dtsAdmin);System.out.println(dtsAdmin);
        if (dtsAdmin==null){
            throw new UnknownAccountException("用户名不存在");
        }

        BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();

        if (!bCryptPasswordEncoder.matches(password,dtsAdmin.getPassword())){
            throw new IncorrectCredentialsException("密码错误");
        }
        if (dtsAdmin.getDeleted()){
            throw new LockedAccountException("账户被锁定，请联系管理员");
        }
        AuthenticationInfo info=new SimpleAuthenticationInfo(dtsAdmin,password,this.getName());
        try {
            String s = InetAddress.getLocalHost().toString();
            s=s.substring(s.lastIndexOf("/")+1);
            dtsAdmin.setLastLoginIp(s);
            dtsAdmin.setLastLoginTime(new Date());
            adminService.updateAdmin(dtsAdmin);
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
        return info;
    }
}
